package com.googlecode.dtools.typeinfo.graphviz.worker.impl;

import com.googlecode.dtools.typeinfo.beans.DmType;
import com.googlecode.dtools.typeinfo.graphviz.bean.DmLink;
import com.googlecode.dtools.typeinfo.graphviz.holder.DmLinkSetHolder;
import com.googlecode.dtools.typeinfo.graphviz.map.Mapper;
import com.googlecode.dtools.typeinfo.graphviz.worker.GraphvizWorker;
import com.googlecode.dtools.typeinfo.work.FilesWorker;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author A_Reshetnikov
 * @since Date: 22.05.12 Time: 17:01
 */
public class GraphvizWorkerImpl implements GraphvizWorker {

    private FilesWorker filesWorker;
    private Mapper mapper;
    private DmLinkSetHolder dmLinkSetHolder;

    public void addListToHolder(List<String> list) {
        for (String typeName : list) {
            addToHolder(typeName);
        }
    }

    public void addToHolder(String typeName) {

        DmType dmType = filesWorker.readXml(typeName);
        List<DmLink> list = mapper.map2DmLinkList(dmType);
        dmLinkSetHolder.addAll(list);

    }


    public void workSingle(String typeName) {
        DmType dmType = filesWorker.readXml(typeName);
        String graphvizText = dmLinkSetHolder.getGraphvizText(typeName);
        if (graphvizText != null)
            dmType.setGraphvizText(dmLinkSetHolder.getGraphvizText(typeName));
        filesWorker.saveXml(dmType);
    }

    public void workList(List<String> list) {
        for (String typeName : list) {
            workSingle(typeName);
        }
    }

    public void setFilesWorker(FilesWorker filesWorker) {
        this.filesWorker = filesWorker;
    }

    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    public void setDmLinkSetHolder(DmLinkSetHolder dmLinkSetHolder) {
        this.dmLinkSetHolder = dmLinkSetHolder;
    }
}
